package com.haois.mp.wechat.config;


import com.haois.mp.wechat.constants.Enum.MenuEnum;
import com.haois.mp.wechat.handler.*;
import me.chanjar.weixin.mp.api.impl.WxMpMaterialServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpMenuServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * 微信公众好服务配置
 */
@Configuration
public class MpConfig {

    @Autowired
    private SubscribeHandler subscribeHandler;
    @Autowired
    private UnSubscribeHandler unSubscribeHandler;
    @Autowired
    private DefaultHandler defaultHandler;
    @Autowired
    private ContactMeHandler contactMeHandler;
    @Autowired
    private MiniProgramHandler miniProgramHandler;
    @Autowired
    private ScanQRHandler scanQRHandler;
    @Autowired
    private TextHandler textHandler;
    @Autowired
    private QuickHandler quickHandler;
    @Autowired
    private ViewHandler viewHandler;
    @Autowired
    private MoreInfoHandler moreInfoHandler;

    @Value("${defined.wxmp.app-id}")
    private String appId;
    @Value("${defined.wxmp.app-secret}")
    private String appSecret;
    @Value("${defined.wxmp.token}")
    private String token;
    @Value("${defined.upload-tmp-url}")
    private String tmpDirFilePath;

    private String partner = "0";
    private String partnerKey = "0";
    private String aesKey = "0";

    /**
     * 微信客户端配置存储
     *
     * @return
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(appId); // 设置微信公众号的appid
        wxMpConfigStorage.setSecret(appSecret); // 设置微信公众号的app corpSecret
        wxMpConfigStorage.setToken(token); // 设置微信公众号的token
        wxMpConfigStorage.setAesKey(aesKey); // 设置微信公众号的EncodingAESKey
		/*wxMpConfigStorage.setPartnerId(partner); // 设置微信支付的商户号
		wxMpConfigStorage.setPartnerKey(partnerKey); // 设置微信支付的商户号*/
        File tmpDirFile = new File(tmpDirFilePath);//微信文件临时保存目录
        wxMpConfigStorage.setTmpDirFile(tmpDirFile);
        return wxMpConfigStorage;
    }

    /**
     * 微信API的Service
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpMaterialService wxMpMaterialService() {
        WxMpMaterialService wxMpMaterialService = new WxMpMaterialServiceImpl(wxMpService());
        return wxMpMaterialService;
    }

    @Bean
    public WxMpMenuService wxMpMenuService() {
        WxMpMenuService wxMpMenuService = new WxMpMenuServiceImpl(wxMpService());
        return wxMpMenuService;
    }

    /**
     * 微信消息路由器，通过代码化的配置，把来自微信的消息交给handler处理
     *
     * @return
     */
    @Bean
    public WxMpMessageRouter wxMpMessageRouter() {
        WxMpMessageRouter wxMpMessageRouter = new WxMpMessageRouter(
                wxMpService());
        return wxMpMessageRouter

                // 订阅事件
                .rule()
                .msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE)
                .async(false)
                .handler(subscribeHandler).end()

                // 退订事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unSubscribeHandler).end()

                //收到视图请求按钮点击事件
                .rule().msgType(WxConsts.EventType.CLICK)
                .event(WxConsts.EventType.VIEW)
                .handler(viewHandler).end()

                //收到快速查找事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.CLICK)
                .eventKey(MenuEnum.BTN_INFO_QUICK.getKey())
                .async(false)
                .handler(quickHandler).end()

                //收到微信扫码事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SCANCODE_WAITMSG)
                .eventKey(MenuEnum.BTN_INFO_SCAN.getKey())
                .async(false)
                .handler(scanQRHandler).end()

                //点击联系我事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.CLICK)
                .eventKey(MenuEnum.BTN_CONTENT_ME.getKey())
                .async(false)
                .handler(contactMeHandler).end()

                //点击小程序事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.CLICK)
                .eventKey(MenuEnum.BTN_MINI_PROGRAM.getKey())
                .async(false)
                .handler(miniProgramHandler).end()

                //收到文本消息
                .rule().msgType(WxConsts.XmlMsgType.TEXT)
                .async(false)
                .handler(textHandler).end()

                //收到更多服务事件
                .rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.CLICK)
                .eventKey(MenuEnum.BTN_INFO_MORE.getKey())
                .async(false)
                .handler(moreInfoHandler).end()

                // 默认规则
                .rule().async(false).handler(defaultHandler).end();
    }

}
